package com.leenmvc.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * Mqtt客户端注入Spring配置信息
 */
@Component
public class MqttConfig {


    @Value("${emq.broker}")
    private String broker;

    @Value("${emq.topic}")
    private String topic;

    @Value("${emq.port}")
    private String port;

    @Value("${emq.qos}")
    private String qos;

    @Value("${emq.userName}")
    private String userName;

    @Value("${emq.password}")
    private String password;



    public String getBroker() {
        return broker;
    }

    public void setBroker(String broker) {
        this.broker = broker;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getQos() {
        return qos;
    }

    public void setQos(String qos) {
        this.qos = qos;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

//    @Bean
//    public MqttClient mqttClient() {
//        MqttClient mqttClient = null;
//        try {
//            log.info("正在连接服务器为：" + broker + port + "的emq消息中间件...");
//            //UUID作为客户端Id
//            clientId= UUID.randomUUID().toString();
//            mqttClient = new MqttClient(broker +":"+ port,clientId , new MemoryPersistence());
//            //设置qos安全等级
//            int[] Qos = new int[]{Integer.parseInt(qos)};
//            //设置mqtt客户端回调信息
//            mqttClient.setCallback(pushCallback(mqttClient,Qos,new String[]{topic}));
//            //设置mqtt客户端连接属性 并连接
//            mqttClient.connect(mqttConnectOptions());
//            //订阅主题
//            //订阅
//            mqttClient.subscribe(new String[]{topic}, Qos);
//            log.info("客户端创建成功");
//        } catch (MqttException e) {
//            log.error(e.getMessage(),e);
//        }
//        return mqttClient;
//    }
//
//    @Bean
//    public MqttConnectOptions mqttConnectOptions() {
//        // MQTT的连接设置
//        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
//        //掉线的时候会清空客户端中的session ， 等待重连的时候创建了一个新的session
//        mqttConnectOptions.setCleanSession(true);
//        //用户名密码认证时候用到
//        mqttConnectOptions.setUserName(userName+clientId);
//        mqttConnectOptions.setPassword(password.toCharArray());
//        mqttConnectOptions.setConnectionTimeout(2000);
//        // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线，但这个方法并没有重连的机制
//        mqttConnectOptions.setKeepAliveInterval(20);
//        //设置断开后重新连接
//        mqttConnectOptions.setAutomaticReconnect(true);
//        return mqttConnectOptions;
//    }
//
//    @Bean
//    public PushCallbackService pushCallback(MqttClient mqttClient, int[] Qos, String[] topics) {
//        return new PushCallbackService(mqttClient,Qos,topics);
//    }
}
